package com.api.wbi.bus.util;

import com.engine.cube.biz.CodeBuilder;
import com.engine.edc.util.DBUtil;
import weaver.formmode.data.ModeDataInterface;
import weaver.formmode.setup.ModeRightInfo;
import weaver.formmode.view.ModeViewLog;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.List;

public class ModeAfterSaveAction {
    private List<String> ids;
    private int formId;
    private int modeId;

    public ModeAfterSaveAction(int modeId, int formId, List<String> ids) {
        this.modeId = modeId;
        this.formId = formId;
        this.ids = ids;
    }

    public void execute() {
        int userId = 1;
        int userLan = 7;
        Integer createExpandId = DBUtil.queryForObject("select id from mode_pageexpand where issystem=1 and issystemflag = ? and modeid = ?",
                Integer.class,1, modeId);
        Integer upateExpandId = DBUtil.queryForObject("select id from mode_pageexpand where issystem=1 and issystemflag = ? and modeid = ?",
                Integer.class,2, modeId);
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        ModeViewLog modeViewLog = new ModeViewLog();
        this.ids.forEach(id->{
            boolean isCreate = true;
            int intId = Util.getIntValue(id);
            if (isCreate) {
                modeRightInfo.setNewRight(true);
                modeRightInfo.editModeDataShare(userId, modeId, intId);//新建的时候添加共享

                CodeBuilder builder = new CodeBuilder(modeId);
                builder.setLanguage(userLan);
                builder.getModeCodeStr(formId, intId);//生成编号
            } else {
                modeRightInfo.rebuildModeDataShareByEdit(userId, modeId, intId);//编辑时重新生成默认共享
            }
            modeRightInfo.addDocShare(userId, modeId, intId);//给文档赋权

            int operateType = isCreate ? 1 : 2;// 1:新建 2:编辑

            if (modeId > 0) {//支持页面扩展
                //执行DML接口
                ModeDataInterface modeDataInterface = new ModeDataInterface();
                modeDataInterface.setFormid(formId);
                modeDataInterface.setBillid(id);
                modeDataInterface.setPageexpandid(isCreate?createExpandId:upateExpandId);
                modeDataInterface.setModeid(modeId);
                modeDataInterface.setUser(new User(1));
                modeDataInterface.execute();
            }

            String operateDesc = isCreate ? SystemEnv.getHtmlLabelName(1421, userLan) :
                    SystemEnv.getHtmlLabelName(33797, userLan);//新增、修改
            int htmllabelindex = 0;//#887673
            htmllabelindex = isCreate ? 1421 : 33797;
            modeViewLog.resetParameter();
            modeViewLog.setClientaddress("");
            modeViewLog.setModeid(modeId);
            modeViewLog.setOperatedesc(operateDesc);
            modeViewLog.setHtmllabelindex(htmllabelindex);
            modeViewLog.setOperatetype(String.valueOf(operateType));
            modeViewLog.setOperateuserid(userId);
            modeViewLog.setRelatedid(intId);
            try {
                modeViewLog.setSysLogInfo();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
